import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
import * as ts from 'typescript'
import { resolveConfig } from '@vue/language-service'
import { createLanguageService } from '@volar/monaco/worker'
import createTsService, {
  createJsDelivrDtsHost
} from 'volar-service-typescript'

self.onmessage = () => {
  worker.initialize((ctx) => {
    const compilerOptions = {
      ...ts.getDefaultCompilerOptions(),
      allowJs: true,
      jsx: ts.JsxEmit.Preserve,
      module: ts.ModuleKind.ESNext,
      moduleResolution: ts.ModuleResolutionKind.NodeJs
    }

    return createLanguageService({
      workerContext: ctx,
      config: resolveConfig(
        {
          services: {
            typescript: createTsService({ dtsHost: createJsDelivrDtsHost() })
          }
        },
        compilerOptions,
        undefined,
        undefined,
        ts
      ),
      typescript: {
        module: ts,
        compilerOptions: compilerOptions
      }
    })
  })
}
